package com.myapp.db.config.define;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.myapp.data.define.BaseBean;

public class IDRecord {
	private static IDRecord idRecord = null;

	private Map<Class<? extends BaseBean>, AtomicLong> ids = new HashMap<Class<? extends BaseBean>, AtomicLong>();

	public static IDRecord getInstance() {
		if (idRecord == null) {
			idRecord = new IDRecord();
		}
		return idRecord;
	}

	public void addTableMaxId(Class<? extends BaseBean> clazz, long maxId) {
		this.ids.put(clazz, new AtomicLong(maxId));
	}

	public long getNextIdByClass(Class<? extends BaseBean> clazz)
			throws Exception {
		if (!this.ids.containsKey(clazz)) {
			throw new IllegalArgumentException(String.format(
					"Can't find bean class[%s]", clazz));
		}
		return this.ids.get(clazz).addAndGet(1);
	}

}
